package com.jikexueyuan.drawingboard;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.media.MediaRecorder;
import com.jikexueyuan.drawingboard.VoiceRecorder;

public class MainActivity extends Activity {
	
	private Button clear_btn;
    private Button rec_btn;
    private Button erase_btn;
    private Button replayScreen_btn;
	private MyView view;
    private VoiceRecorder recorder;
    private boolean recEnable;
    private boolean eraserEnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recorder = new VoiceRecorder(this, "voice", "1");
        clear_btn = (Button) findViewById(R.id.clear_btn);
        rec_btn = (Button) findViewById(R.id.rec_btn);
        erase_btn = (Button) findViewById(R.id.erase_btn);
        replayScreen_btn = (Button) findViewById(R.id.replayscreen_btn);
        view = (MyView) findViewById(R.id.draw);
        recEnable = false;
        eraserEnable = false;

        clear_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				view.clear();
                view.resetRecordPoints();
			}
		});

        rec_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    if(!recEnable) {
                        recorder.startRecording();
                        recEnable = true;
                        rec_btn.setText("停止");
                        view.setRecordEnable();
                        view.saveStartTime();
                    }
                    else {
                        recorder.stopRecording();
                        recorder.saveToDB();
                        recEnable = false;
                        rec_btn.setText("录屏");
                        view.setRecordDisable();
                    }

                }
                catch (Exception e) {
                    System.out.println("Exception: Recording: " + e );
                }
            }
        });

        erase_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if(!eraserEnable) { // 选中橡皮擦
                    view.selectPaintStyle(0);
                    eraserEnable = true;
                    erase_btn.setText("画笔");
                }
                else { // 选中画笔
                    view.selectPaintStyle(1);
                    eraserEnable = false;
                    erase_btn.setText("橡皮");
                }
            }
        });

        replayScreen_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                view.replayPenTrack(recorder);
            }
        });
    }

}
